app.controller('PaymentListCtrl', function ($scope, ajax, $modal, toaster) {

	$scope.paymentList = [
		{ id: 1, name: "支付宝" },
		{ id: 2, name: "微信支付" },
		{ id: 3, name: "微信公众号" },
	];
	$scope.imList = [
		{ id: 1, name: "电子客票" },
		{ id: 2, name: "取票码" }
	];

	$scope.summary = [];
	$scope.doSummary = function () {
		ajax.call(new Req.GET({
			params: $scope.queryMap,
			resource: 'payment/1/summary'
		})).then(function (result) {
			$scope.summary = result.result;
			$scope.totalCount = $scope.summary[0]; //支付流水数
			$scope.charge = $scope.summary[1]; //应付金额
			$scope.pay = $scope.summary[2]; //实付金额
			$scope.insurances = $scope.summary[3]; //保险金额
			$scope.tickets = $scope.summary[4]; //票数
			$scope.flag = true;
		}, function () { });
	};

	$scope.reload = $scope.page.loader('payment/1/{pageIndex}/{pageSize}/page');
	$scope.$on('pageIndex', function (event, p) {
		$scope.reload(p, $scope.queryMap);
		$scope.doSummary();
	});
	$scope.reload(1);
	$scope.doSummary();

	//模糊查询
	$scope.queryMap = {};
	$scope.timeRange = {};
	$scope.query = function () {
		if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
			var starTime = $scope.timeRange.startTime.pattern("yyyy-MM-dd");
			var endTime = $scope.timeRange.endTime.pattern("yyyy-MM-dd");
			$scope.queryMap.payTime = starTime + "#@" + endTime;
		} else if ($scope.timeRange.startTime == null || $scope.timeRange.endTime == null) {
			delete $scope.queryMap.payTime;
		}

		if ($scope.starList.selected != undefined) {
			// $scope.queryMap.order__orderDetail__startStation = $scope.starList.selected.name;
			$scope.queryMap.stationNo = $scope.starList.selected.stationNo;
		} else {
			// delete $scope.queryMap.order__orderDetail__startStation;
			delete $scope.queryMap.stationNo;
		}

		if ($scope.paymentList.selected != undefined) {
			$scope.queryMap.payment__type = $scope.paymentList.selected.id;
		} else if ($scope.paymentList.selected == undefined) {
			delete $scope.queryMap.payment__type;
		}
		if ($scope.imList.selected) {
			if ($scope.imList.selected.id == 1) {
				$scope.queryMap.order__orderDetail__immediate = true;
			} else if ($scope.imList.selected.id == 2) {
				$scope.queryMap.order__orderDetail__immediate = false;
			}
		} else {
			delete $scope.queryMap.order__orderDetail__immediate;
		}

		$scope.reload(1, $scope.queryMap);
		$scope.doSummary();
	};

	//导出Excel
	$scope.exportExcel = function () {
		if ($scope.starList.selected) {
			// $scope.queryMap.order__orderDetail__startStation = $scope.starList.selected.name;
			$scope.queryMap.stationNo = $scope.starList.selected.stationNo;
		} else {
			// delete $scope.queryMap.order__orderDetail__startStation;
			delete $scope.queryMap.stationNo;
		}

		if ($scope.paymentList.selected) {
			$scope.queryMap.payment__type = $scope.paymentList.selected.id;
		} else {
			delete $scope.queryMap.payment__type;
		}

		if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
			var starTime = $scope.timeRange.startTime.pattern("yyyy-MM-dd");
			var endTime = $scope.timeRange.endTime.pattern("yyyy-MM-dd");
			$scope.queryMap.payTime = starTime + "#@" + endTime;
		} else {
			delete $scope.queryMap.payTime;
		}
		var fields = $scope.queryMap;
		var url = 'payment/bk/export';
		ajax.call(new Req.POST({ fields: fields, resource: url }))
			.then(function (result) {
				if (result.code == 200) {
					window.open(result.result);
					toaster.pop('success', '成功', '导出信息成功。');
				} else {
					toaster.pop('error', '错误', '导出信息失败！！');
				}
			}, function () { toaster.pop('error', '错误', '导出信息失败！'); });
	};

	//起始客运站列表读取
	$scope.starList = [];
	(function () {
		ajax.call(new Req.GET({
			resource: 'starting/bk/list'
		})).then(function (result) {
			$scope.starList = result.result;
		}, function () { });
	})();
});
